<!--
// Copyright © 2020, 2021 Anticrm Platform Contributors.
// Copyright © 2021, 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
  import { Attachment } from '@hcengineering/attachment'
  import { BlobType, WithLookup } from '@hcengineering/core'
  import { ListSelectionProvider } from '@hcengineering/view-resources'
  import { createEventDispatcher } from 'svelte'
  import { AttachmentImageSize } from '../types'
  import { getType, isAttachment, showAttachmentPreviewPopup } from '../utils'
  import AttachmentActions from './AttachmentActions.svelte'
  import AttachmentImagePreview from './AttachmentImagePreview.svelte'
  import AttachmentPresenter from './AttachmentPresenter.svelte'
  import AttachmentVideoPreview from './AttachmentVideoPreview.svelte'
  import AudioPlayer from './AudioPlayer.svelte'

  export let value: WithLookup<Attachment> | BlobType
  export let isSaved: boolean = false
  export let listProvider: ListSelectionProvider | undefined = undefined
  export let imageSize: AttachmentImageSize = 'auto'
  export let removable: boolean = false
  export let videoPreload = false

  const dispatch = createEventDispatcher()

  let hovered = false

  $: type = getType(value.type)
</script>

{#if type === 'video'}
  <div class="content buttonContainer flex-center" class:hovered>
    <AttachmentVideoPreview {value} preload={videoPreload} />
    <div class="actions">
      <AttachmentActions bind:hovered attachment={value} {isSaved} {removable} />
    </div>
  </div>
{:else if value.metadata?.thumbnail !== undefined || type === 'image'}
  <!-- svelte-ignore a11y-click-events-have-key-events -->
  <!-- svelte-ignore a11y-no-static-element-interactions -->
  <div
    class="content flex-center buttonContainer cursor-pointer"
    class:hovered
    on:click={() => {
      if (listProvider !== undefined && isAttachment(value)) listProvider.updateFocus(value)
      const popupInfo = showAttachmentPreviewPopup(value)
      dispatch('open', popupInfo.id)
    }}
  >
    <AttachmentImagePreview {value} size={imageSize} />
    <div class="actions">
      <AttachmentActions bind:hovered attachment={value} {isSaved} {removable} />
    </div>
  </div>
{:else if type === 'audio'}
  <div class="buttonContainer" class:hovered>
    <AudioPlayer {value} />
    <div class="actions" style:padding={'0.125rem 0.25rem'}>
      <AttachmentActions attachment={value} {isSaved} {removable} />
    </div>
  </div>
{:else}
  <div class="flex buttonContainer extraWidth" class:hovered>
    <AttachmentPresenter {value} />
    <div class="actions">
      <AttachmentActions bind:hovered attachment={value} {isSaved} {removable} />
    </div>
  </div>
{/if}

<style lang="scss">
  .buttonContainer {
    position: relative;
    width: fit-content;
    height: fit-content;

    &.extraWidth {
      padding-right: 2.5rem;
    }

    .actions {
      visibility: hidden;
      position: absolute;
      top: 0.25rem;
      right: 0.25rem;
      padding: 0.125rem;
      background-color: var(--theme-comp-header-color);
      border: 1px solid var(--theme-divider-color);
      border-radius: 0.375rem;
    }
  }

  .buttonContainer {
    &:hover,
    &.hovered {
      .actions {
        visibility: visible;
      }
    }
  }

  .content {
    max-width: 25rem;
    max-height: 25rem;
    scroll-snap-align: start;
  }
</style>
